package com.itheima.advice;

import com.itheima.mapper.EmpLoginMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.EmpDto;
import com.itheima.pojo.EmpLoginLog;
import com.itheima.pojo.LoginInfo;
import com.itheima.pojo.Result;
import com.itheima.utils.LoginException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Aspect
@Slf4j
public class RecordEmpLoginLog {
//    @Autowired
//    private EmpLoginMapper empLoginMapper;
    @Autowired
    private EmpLoginMapper empLoginMapper;
    @Around("@annotation(com.itheima.advice.EmpLoginLogAnno)")
    public Object recordEmpLog(ProceedingJoinPoint pjp) throws Throwable {
//        Object[] args = pjp.getArgs();
//        EmpDto empDto = (EmpDto) args[0];
//        EmpLoginLog empLoginLog = new EmpLoginLog();
//        empLoginLog.setUsername(empDto.getUsername());
//        empLoginLog.setPassword(empDto.getPassword());
//        empLoginLog.setLoginTime(LocalDateTime.now());
//
//        long begin = System.currentTimeMillis();
//        long end = 0;
//        Object result =null;
//        try {
//            result = pjp.proceed();
//            end = System.currentTimeMillis();
//
//            empLoginLog.setIsSuccess((short) 1);
//            LoginInfo loginInfo =  (LoginInfo) result;
//            empLoginLog.setJwt(loginInfo.getToken());
//            empLoginLog.setCostTime(end-begin);
//
//            empLoginMapper.save(empLoginLog);
//
//            return result;
//        } catch (Throwable e) {
//            end = System.currentTimeMillis();
//            empLoginLog.setIsSuccess((short) 0);
//            empLoginLog.setJwt(null);
//            empLoginLog.setCostTime(end-begin);
//
//            empLoginMapper.save(empLoginLog);
//
//            throw new LoginException("用户名或密码错误");
//        }
//
//
//
//
        long begin = System.currentTimeMillis();
        Result result = null;
        Exception exception = null;
        try {
            result = (Result) pjp.proceed();
        } catch (Exception e) {
            exception = e;
        }
        long end = System.currentTimeMillis();
        long costTime = end - begin;
        Object[] args = pjp.getArgs();
        EmpDto empDto = (EmpDto) args[0];
        String username = empDto.getUsername();
        String password = empDto.getPassword();

        EmpLoginLog empLoginLog = new EmpLoginLog();
        if (exception == null && result != null) {
            short code = result.getCode().shortValue();
            LoginInfo data = (LoginInfo) result.getData();
            String token = data.getToken();
            empLoginLog.setUsername(username);
            empLoginLog.setPassword(password);
            empLoginLog.setLoginTime(LocalDateTime.now());
            empLoginLog.setIsSuccess(code);
            empLoginLog.setJwt(token);
            empLoginLog.setCostTime(costTime);
            empLoginMapper.insert(empLoginLog);
        }
        if (exception != null && result ==null ) {
            empLoginLog.setUsername(username);
            empLoginLog.setPassword(password);
            empLoginLog.setIsSuccess((short)0 );
            empLoginLog.setLoginTime(LocalDateTime.now());
            empLoginLog.setCostTime(costTime);
            empLoginMapper.insert(empLoginLog);
            throw exception;
        }
        return result;
    }
}
